<?php
require_once("../libs/string/libstr.php");
require_once("../libs/dbclass/dbconnect.php");
$mc=new libstr();
require_once 'Excel/reader.php';
$ex=$_POST["ex"];


$data = new Spreadsheet_Excel_Reader();
//$exarr=array("xxyw.xls","xxsw.xls","xxyy.xsl","czyw.xls","czsx.xls","czyy.xls","czwl.xls","czhx.xls","czsw.xls","czdl.xls","czls.xls","czzz.xls","gzyw.xls","gzsx.xls","gzyy.xls","gzwl.xls","gzhx.xls","gzsw.xls","gzdl.xls","gzls.xls","gzzz.xls");
//for($i=0;$i<count($exarr);$i++){
	
	




$extext="czls.xls";//读文档

// Set output Encoding.
$data->setOutputEncoding('UTF-8');//支持编码

$data->read($extext);

error_reporting(E_ALL ^ E_NOTICE);
$sz=array();
$arr=$data->sheets[0]["cells"];
$k=0;
//print_r($arr) ;
for($i=2;$i<count($arr)+1;$i++){
$sz[$k]["nianjiID"]=getclassID("nianji_class",$arr[$i][1]);	
$sz[$k]["xueduanID"]=getclassID("xueduan_class",$arr[$i][2]);	
$sz[$k]["xueqiID"]=getclassID("xueqi_class",$arr[$i][3]);
$sz[$k]["xuekeID"]=getclassID("xueke_class",$arr[$i][4]);	
$sz[$k]["root1"]=qudiaozimu($arr[$i][5]);
$sz[$k]["root2"]=qudiaozimu($arr[$i][6]);	
$sz[$k]["root3"]=qudiaozimu($arr[$i][7]);	
$sz[$k]["root4"]=qudiaozimu($arr[$i][8]);	
$sz[$k]["root5"]=qudiaozimu($arr[$i][9]);
		
$k++;
}//得出一个常规数组
//去掉类名坑爹的字母

$aa=getchild1($sz);//获得最终数组
print_r($aa);
//exit();
insert_data($aa);//插入数据
//}
function qudiaozimu($str){

	 $string= addslashes($str);
	if(preg_match('/[A-Z]+/',$str)){
	 	$string=substr($string,0,strlen($string)-7);
	 }
	return $string;
}
//获得子集的开始行
function getstart($arr,$root,$type=1){
	$k=0;
		$rootid="root".$type;

	for($i=0;$i<count($arr);$i++){
	if($root==$arr[$i][$rootid])	{
		
		$k=$i;
		//$k=100;
	}
	}
	return $k;
}

//获得子集结束值
function getend($arr,$root,$type=1){
	$k=0;
	$rootid="root".$type;

	$sline=getstart($arr,$root,$type);
		for($i=$sline;$i<count($arr);$i++){
	if($root==$arr[$i][$rootid]){
			for($z=$i+1;$z<count($arr);$z++){
			if($arr[$z][$rootid]!=""){
				return $z;
			}
		}
		
	}
	}

return count($arr);
}

function getchild1($arr){//获得终极数组
	$a=array();
	$k=0;
	foreach ($arr as $v){
	//if($k>$sline ){
	if($v["root1"]!=""){
		
	$sline=getstart($arr,$v["root1"]);
		$eline=getend($arr,$v["root1"]);
		
		$a[$k]["classname"]= $v["root1"];
		$a[$k]["xuekeID"]= $v["xuekeID"];
		$a[$k]["nianjiID"]= $v["nianjiID"];
		$a[$k]["xueqiID"]= $v["xueqiID"];	
		$a[$k]["xueduanID"]= $v["xueduanID"];
			$a[$k]["child"]=getchild2($arr,$sline,$eline);
			$k++;
	}
	//}
		
	}
	return $a;
}

function getchild2($arr,$sline,$eline){
	$a=array();
	$k=0;
	$z=0;
	foreach ($arr as $v){
		if($k>$sline && $k<$eline){
	if($v["root2"]!=""){
		$sline1=getstart($arr,$v["root2"],2);
		$eline1=getend($arr,$v["root2"],2);
			$a[$z]["classname"]= $v["root2"];
			$a[$z]["xuekeID"]= $v["xuekeID"];
		$a[$z]["nianjiID"]= $v["nianjiID"];
		$a[$z]["xueqiID"]= $v["xueqiID"];	
	$a[$z]["xueduanID"]= $v["xueduanID"];
			$a[$z]["child"]=getchild3($arr,$sline1,$eline1);
	
		$z++;
	}
		}
		$k++;
	}
	return $a;
}
//print_r(getchild3($sz,0,5));
function getchild3($arr,$sline,$eline){
	$a=array();
	$k=0;
	$z=0;
	foreach ($arr as $v){
	if($k>$sline && $k<$eline){
		if($v["root3"]!=""){
			$sline2=getstart($arr,$v["root3"],3);
		$eline2=getend($arr,$v["root3"],3);
		$a[$z]["classname"]= $v["root3"];
		$a[$z]["xuekeID"]= $v["xuekeID"];
		$a[$z]["nianjiID"]= $v["nianjiID"];
		$a[$z]["xueqiID"]= $v["xueqiID"];
	$a[$z]["xueduanID"]= $v["xueduanID"];	
			$a[$z]["child"]=getchild4($arr,$sline2,$eline2);
			$z++;	
		}
			
	}
	$k++;	
	}
	return $a;
}

function getchild4($arr,$sline,$eline){
	$a=array();
	$k=0;
	$z=0;
	foreach ($arr as $v){
	if($k>$sline && $k<$eline){
		if($v["root4"]!=""){
		$sline3=getstart($arr,$v["root4"],4);
		$eline3=getend($arr,$v["root4"],4);
		$a[$z]["classname"]= $v["root4"];
		$a[$z]["xuekeID"]= $v["xuekeID"];
		$a[$z]["nianjiID"]= $v["nianjiID"];
		$a[$z]["xueqiID"]= $v["xueqiID"];
		$a[$z]["xueduanID"]= $v["xueduanID"];	
			$a[$z]["child"]=getchild5($arr,$sline3,$eline3);
			$z++;	
		}
			
	}
	$k++;	
	}
	return $a;
}

function getchild5($arr,$sline,$eline){
	$a=array();
	$k=0;
	$z=0;
	foreach ($arr as $v){
	if($k>$sline && $k<$eline){
		if($v["root5"]!=""){
		$sline4=getstart($arr,$v["root5"],5);
		$eline4=getend($arr,$v["root5"],5);
		$a[$z]["classname"]= $v["root5"];
		$a[$z]["xuekeID"]= $v["xuekeID"];
		$a[$z]["nianjiID"]= $v["nianjiID"];
		$a[$z]["xueqiID"]= $v["xueqiID"];
		$a[$z]["xueduanID"]= $v["xueduanID"];	
			$a[$z]["child"]=array();
			$z++;	
		}
			
	}
	$k++;	
	}
	return $a;
}




//功能函数把类名转化为ID
function getclassID($tb,$classname){
	global $db;
	$classid=$db->dvstr("select * from ".$tb." where classname='".$classname."'");
	return  $classid["ID"];
	
}


///*$arr要插入的数组;$rootid父级的rootid;$pid父id*/
function insert_data($arr,$rootid="0,",$pid=0){
		global $db;
		
foreach($arr as $v){
	$data=array();
	$data["classname"]=$v["classname"];
	$data["nianjiID"]=$v["nianjiID"];
	$data["xueqiID"]=$v["xueqiID"];
	$data["xuekeID"]=$v["xuekeID"];
	$data["xueduanID"]=$v["xueduanID"];
	$data["pid"]=$pid;

	$db->add_data("test_class",$data);
	$lastid=$db->insert_id();//获得上一插入的id
     $rootid1=$rootid.$lastid.",";
   $db->cmd("update test_class set rootid='".$rootid1."' where id=".$lastid);
    if($v["child"]!=null){
   insert_data($v["child"],$rootid1,$lastid);
    	
    }

}	
	
}




?>
<form action="" method="post">
<select name="ex">

<option value="">选择数据</option>
<option value="xxyw.xls">小学语文</option>
<option value="xxsx.xls">小学数学</option>
<option value="xxyy.xls">小学英语</option>
</select>
<input name="insertdata" type="submit"  value="导入数据"/>
</form>